package com.j48.entrancegurad.controller;

import com.j48.entrancegurad.model.Admin;
import com.j48.entrancegurad.model.Character;
import com.j48.entrancegurad.model.Notice;
import com.j48.entrancegurad.service.IAdminService;
import com.j48.entrancegurad.service.INoticeService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

@Controller
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IAdminService adminService;

    /**
     * 添加公告
     * @param req
     * @param notice
     * @return
     */
    @RequestMapping("/insertnotice")
    @ResponseBody
    public String insertNotice(HttpServletRequest req, Notice notice){
        String noticecontent=req.getParameter("notice_content");
        Admin admin1=(Admin)req.getSession().getAttribute("admin");
        String noticeadmin=admin1.getAdmin_name();
        Timestamp createtime=new Timestamp(new Date().getTime());
        Admin admin=new Admin();
        ArrayList<Admin> adminlist=new ArrayList<Admin>();
        admin.setAdmin_name(noticeadmin);
        adminlist=adminService.selectByUserNames(admin);
        notice.setNotice_content(noticecontent);
        notice.setNotice_admin_id(adminlist.get(0).getAdmin_id());
        notice.setNotice_createtime(createtime);
        notice.setNotice_lasttime(createtime);
        noticeService.insertNotice(notice);
        return "true";
    }

    @RequestMapping("/findadmin")
    @ResponseBody
    public void findadminId(HttpServletResponse resp, HttpServletRequest req, Admin admin) throws IOException {
        PrintWriter w=resp.getWriter();
        String adminname=req.getParameter("notice_admin_name");
        admin.setAdmin_name(adminname);
        ArrayList<Admin> admin1=new ArrayList<Admin>();
        admin1=adminService.selectByUserNames(admin);
        if(admin1.size()==0){
            w.write("false");
        }else{
            w.write("true");
        }
    }

    /**
     * 删除公告
     * @param req
     * @param notice
     * @return
     */
    @RequestMapping("/deletenotice")
    @ResponseBody
    public String deleteById(HttpServletRequest req,Notice notice){
        int noticeid=Integer.parseInt(req.getParameter("nid"));
        notice.setNotice_id(noticeid);
        noticeService.deleteById(notice);
        return "true";
    }

    /**
     * 摧毁session
     * @param req
     * @return
     */
    @RequestMapping("/flushnotice")
    @ResponseBody
    public String flushpage(HttpServletRequest req){
        req.getSession().removeAttribute("noticelist");
        req.getSession().removeAttribute("noticeNames");
        ArrayList<Notice> noticelist=new ArrayList<Notice>();
        noticelist=noticeService.selectAll();
        ArrayList<Integer> adminid=new ArrayList<Integer>();
        ArrayList<String> noticeNames=new ArrayList<String>();
        Admin noticeAdmin=new Admin();
        Admin noticeAdmin1=new Admin();
        for(int i=0;i<noticelist.size();i++){
            adminid.add(i,noticelist.get(i).getNotice_admin_id());
        }
        for(int i=0;i<adminid.size();i++){
            noticeAdmin.setAdmin_id(adminid.get(i));
            noticeAdmin1=adminService.selectById(noticeAdmin);
            noticeNames.add(noticeAdmin1.getAdmin_name());
        }
        Collections.reverse(noticelist);
        Collections.reverse(noticeNames);
        req.getSession().setAttribute("noticelist",noticelist);
        req.getSession().setAttribute("noticeNames",noticeNames);
        return "true";
    }

    @RequestMapping("/selectnoticeName")
    @ResponseBody
    public ModelAndView selectById(HttpServletRequest req, Notice notice,ModelAndView model,Admin admin){
        model=new ModelAndView("jsp/notice");
        String noticeName=req.getParameter("noticeName");
        ArrayList<Notice> noticelist=new ArrayList<Notice>();
        ArrayList<Notice> noticelistsmall=new ArrayList<Notice>();
        ArrayList<Admin> adminlist=new ArrayList<Admin>();
        ArrayList<String> noticeNames=new ArrayList<String>();
        ArrayList<Integer> adminIds=new ArrayList<Integer>();
        Notice notice1=new Notice();
        Admin admin1=new Admin();
        Admin admin2=new Admin();
        if(noticeName==""){
            noticelist=noticeService.selectAll();
            for(int i=0;i<noticelist.size();i++){
                admin.setAdmin_id(noticelist.get(i).getNotice_admin_id());
                admin1= adminService.selectById(admin);
                if(admin1!=null){
                    noticeNames.add(i,admin1.getAdmin_name());
                }else{
                    noticeNames.add(i,"无");
                }
            }
            Collections.reverse(noticelist);
            Collections.reverse(noticeNames);
            req.getSession().setAttribute("noticelist",noticelist);
            req.getSession().setAttribute("noticeNames",noticeNames);
            return model;
        }else{
            admin.setAdmin_name(noticeName);
            adminlist=adminService.selectByUserNames(admin);
            if(adminlist.size()==0){
                Collections.reverse(noticelist);
                Collections.reverse(noticeNames);
                req.getSession().removeAttribute("noticelist");
                req.getSession().removeAttribute("noticeNames");
                return model;
            }else{
                for(int i=0;i<adminlist.size();i++){
                    adminIds.add(i,adminlist.get(i).getAdmin_id());
                }
                for(int i=0;i<adminIds.size();i++){
                    notice1.setNotice_admin_id(adminIds.get(i));
                    admin1.setAdmin_id(adminIds.get(i));
                    admin2=adminService.selectById(admin1);
                    noticelistsmall=noticeService.selectByAdminId(notice1);
                    for(int k=0;k<noticelistsmall.size();k++){
                        noticelist.add(noticelistsmall.get(k));
                        noticeNames.add(admin2.getAdmin_name());
                    }
                }
                Collections.reverse(noticelist);
                Collections.reverse(noticeNames);
                req.getSession().setAttribute("noticelist",noticelist);
                req.getSession().setAttribute("noticeNames",noticeNames);
            }
        }
        return model;
    }
}
